﻿using System;
using System.Collections.Generic;
using System.Linq;
using GeoAPI.Geometries;
using HYCodeBase.Geometry;
using NetTopologySuite.Geometries;

namespace HYCodeBase.TopologySuite
{
	internal static class NTSTransformer
	{
		internal static bool TryChangeLineStringToHYMulPointXYZ(LineString inputLineString, out HYMulPointXYZ creatResult)
		{
			creatResult = null;
			if (null == inputLineString)
			{
				return false;
			}
			List<HYPointXYZ> list = new List<HYPointXYZ>();
			foreach (Coordinate coordinate in inputLineString.Coordinates)
			{
				list.Add(new HYPointXYZ(coordinate.X, coordinate.Y, coordinate.Z));
			}
			creatResult = new HYMulPointXYZ(list);
			return creatResult != null;
		}

		internal static bool TryChangePolygonToHYPolygon(Polygon input, out HYPolygon creatResult)
		{
			creatResult = null;
			if (null == input)
			{
				return false;
			}
			HYMulPointXYZ inputShell = null;
			if (!NTSTransformer.TryChangeLineStringToHYMulPointXYZ(input.Shell as LineString, out inputShell))
			{
				return false;
			}
			List<HYMulPointXYZ> list = null;
			if (input.Holes.Count<ILinearRing>() > 0)
			{
				list = new List<HYMulPointXYZ>();
				HYMulPointXYZ item = null;
				ILinearRing[] holes = input.Holes;
				for (int i = 0; i < holes.Length; i++)
				{
					if (!NTSTransformer.TryChangeLineStringToHYMulPointXYZ(holes[i] as LineString, out item))
					{
						return false;
					}
					list.Add(item);
				}
			}
			creatResult = new HYPolygon(inputShell, list);
			return creatResult != null;
		}

		internal static bool TryChangePolygonToHYPolygon(MultiPolygon input, out List<HYPolygon> creatResult)
		{
			creatResult = null;
			if (null == input)
			{
				return false;
			}
			creatResult = new List<HYPolygon>();
			HYPolygon item = null;
			for (int i = 0; i < input.NumGeometries; i++)
			{
				if (input.GetGeometryN(i) is Polygon)
				{
					if (!NTSTransformer.TryChangePolygonToHYPolygon(input.GetGeometryN(i) as Polygon, out item))
					{
						return false;
					}
					creatResult.Add(item);
				}
			}
			return creatResult != null;
		}
	}
}
